<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Admin;

use Srv\Apps\Apis\Admin\Ctl\AdminCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Admin\AdmMePassEditDTO;
use Srv\Apps\Proto\DTO\Admin\AdmUserAddDTO;
use Srv\Apps\Proto\DTO\Admin\AdmUserAuthEditDTO;
use Srv\Apps\Proto\DTO\Admin\AdmUserEditDTO;
use Srv\Apps\Proto\DTO\Admin\AdmUserIdDTO;
use Srv\Apps\Proto\DTO\Admin\AdmUserInfoListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Admin\AdmUserInfoListVO;
use Srv\Apps\Proto\VO\Admin\AdmUserInfoVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\AuthData;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class AdminAct extends ActionAbstract
{
    /**
     * @url /adm/me/info
     * @auth admMeInfo
     * @flag REQ_LOG=0
     * @rsp Srv.Apps.Proto.VO.Admin.AdmUserInfoVO
     * 当前管理员信息
     */
    public function getAdmMeInfo(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData, $DTOClass);
        if(!($VOClass instanceof AdmUserInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $admUserId      = AuthData::getLoginUserId();
        $VOClass        = AdminCtl::tag()->getAdmUserInfo($admUserId);
        if(!($VOClass instanceof AdmUserInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /adm/me/pass/edit
     * @auth admMePassEdit
     * @req Srv.Apps.Proto.DTO.Admin.AdmMePassEditDTO
     * 当前管理员修改密码
     */
    public function admMePassEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData, $VOClass);
        if(!($DTOClass instanceof AdmMePassEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $passOld        = $DTOClass->getPasswordOld();
        $passNew        = $DTOClass->getPasswordNew();
        $admUserId      = AuthData::getLoginUserId();
        $errMsg         = '';
        $result         = AdminCtl::tag()->admMePassEdit($admUserId, $passOld, $passNew, $errMsg);
        $LogCtl         = LogCtl::tag();
        $logData        = ['admUserId' => $admUserId, 'passOld' => $passOld, 'passNew' => $passNew];
        if(!$result){
            $LogCtl->logAdd(LogCtl::ADMIN_ME_PASS_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '修改成功';
        $LogCtl->logAdd(LogCtl::ADMIN_ME_PASS_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /adm/user/info
     * @auth admUserInfo
     * @req Srv.Apps.Proto.DTO.Admin.AdmUserIdDTO
     * @rsp Srv.Apps.Proto.VO.Admin.AdmUserInfoVO
     * 管理员详情
     */
    public function getAdmUserInfo(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData);
        if(!($DTOClass instanceof AdmUserIdDTO) || !($VOClass instanceof AdmUserInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $admUserId      = $DTOClass->getAdmUserId();
        $VOClass        = AdminCtl::tag()->getAdmUserInfo($admUserId);
        if(!($VOClass instanceof AdmUserInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /adm/user/list
     * @auth admUserList
     * @req Srv.Apps.Proto.DTO.Admin.AdmUserInfoListDTO
     * @rsp Srv.Apps.Proto.VO.Admin.AdmUserInfoListVO
     * 管理员列表
     */
    public function getAdmUserList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof AdmUserInfoListDTO) || !($VOClass instanceof AdmUserInfoListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AdminCtl::tag()->getAdmUserInfoList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /adm/user/add
     * @auth admUserAdd
     * @req Srv.Apps.Proto.DTO.Admin.AdmUserAddDTO
     * 管理员添加
     */
    public function admUserAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdmUserAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $admUserId      = AdminCtl::tag()->admUserAdd($setData);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        if($admUserId < 1){
            $errMsg     = '管理员新增失败';
            $LogCtl->logAdd(LogCtl::ADMIN_USER_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '管理员新增成功';
        $LogCtl->logAdd(LogCtl::ADMIN_USER_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /adm/user/edit
     * @auth admUserEdit
     * @req Srv.Apps.Proto.DTO.Admin.AdmUserEditDTO
     * 管理员编辑
     */
    public function admUserEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdmUserEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $AdminCtl       = AdminCtl::tag();
        $admUserId      = intval($DTOClass->getAdmUserId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $admUserInfoSrc = $AdminCtl->getAdmUserInfoSrc($admUserId);
        $LogCtl         = LogCtl::tag();
        if(!isset($admUserInfoSrc['auir_id'])){
            $errMsg     = '管理员不存在';
            $logData    = $setData;
            $LogCtl->logAdd(LogCtl::ADMIN_USER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData        = ['new' => $setData, 'old' => $admUserInfoSrc];
        $result         = $AdminCtl->admUserEdit($admUserId, $setData);
        if(!$result){
            $errMsg     = '管理员编辑失败';
            $LogCtl->logAdd(LogCtl::ADMIN_USER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '管理员编辑成功';
        $LogCtl->logAdd(LogCtl::ADMIN_USER_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /adm/user/auth/edit
     * @auth admUserAuthEdit
     * @req Srv.Apps.Proto.DTO.Admin.AdmUserAuthEditDTO
     * 管理员权限编辑
     */
    public function admUserAuthEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdmUserAuthEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $AdminCtl           = AdminCtl::tag();
        $admUserId          = intval($DTOClass->getAdmUserId());
        $setData            = $RouteConfData->getParamsResData($DTOClass);
        $admUserInfoSrc     = $AdminCtl->getAdmUserInfoSrc($admUserId);
        $LogCtl             = LogCtl::tag();
        if(!isset($admUserInfoSrc['auir_id'])){
            $errMsg         = '管理员不存在';
            $logData        = $setData;
            $LogCtl->logAdd(LogCtl::ADMIN_AUTH_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData            = ['new' => $setData, 'old' => $admUserInfoSrc];
        $routeIdList        = $setData[AdmUserAuthEditDTO::_routeIdList];
        $roleIdList         = $setData[AdmUserAuthEditDTO::_roleIdList];
        $result             = $AdminCtl->admUserAuthEdit($admUserId, $routeIdList, $roleIdList);
        if(!$result){
            $errMsg         = '管理员权限编辑失败';
            $LogCtl->logAdd(LogCtl::ADMIN_AUTH_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg             = '管理员权限编辑成功';
        $LogCtl->logAdd(LogCtl::ADMIN_AUTH_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}